<template>
	<view class="container">
		<u-navbar :is-back="true" back-icon-color="white" :title="title" title-color="white" :background="background"></u-navbar>
		<view class="u-m-20 content">
			<u-form>
				<u-form-item label="金额">
					<u-input v-model="form.change" />
				</u-form-item>
				<u-form-item label="类型">
					<u-input v-model="form.chinese_type" type="select" @click="type_show = true" placeholder="选择类型"/>
					<u-action-sheet :list="typeList" v-model="type_show" @click="selectType"></u-action-sheet>
				</u-form-item>
				<u-form-item label="项目">
					<u-input v-model="form.desc" type="select" @click="desc_show = true" placeholder="选择项目" />
					<u-action-sheet :list="descList" v-model="desc_show" @click="selectDesc"></u-action-sheet>
				</u-form-item>
				<u-button type="primary" @click="submit">确定</u-button>
			</u-form>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: "添加账单",
				background: {
					backgroundImage: "linear-gradient(to right, #ff8177 0%, #ff867a 0%, #ff8c7f 21%, #f99185 52%, #cf556c 78%, #b12a5b 100%)"
				},
				typeList: [{
						value: 1,
						text: "收入"
					},
					{
						value: 2,
						text: "支出"
					}
				],
				type_show: false,
				desc_show: false,
				form:{
					change:null,
					type:null,
					chinese_type:null,
					desc:null
				},
				descList: [
					{
						text: "早餐"
					},
					{
						text: "午餐"
					},
					{
						text: "晚餐"
					},
					{
						text: "夜宵"
					},
					{
						text: "其它"
					},
				],
			}
		},
		onLoad() {

		},
		methods: {
			selectType(index) {
				let chinese_type = this.typeList[index].text
				this.form.chinese_type = chinese_type
				let type = this.typeList[index].value
				this.form.type = type
			},
			selectDesc(index) {
				let desc = this.descList[index].text
				this.form.desc = desc
			},
			submit() {
				let that = this
				if (!this.form.change) {
					uni.showToast({
						title: '金额必须填写',
						icon: "none",
						mask: true
					});
					return false;
				}
				if (!this.form.type) {
					uni.showToast({
						title: '类型必须选择',
						icon: "none",
						mask: true
					});
					return false;
				}
				if (!this.form.desc) {
					uni.showToast({
						title: '项目必须选择',
						icon: "none",
						mask: true
					});
					return false;
				}
				let income = 0
				let consume = 0
				let op_change = 0
				let change = this.form.change
				if (this.form.type == 1) {
					income = change
					op_change = "+" + change
				} else {
					consume = change
					op_change = "-" + change
				}
				let desc = this.form.desc
				let data = {
					id: uni.getStorageSync("id"),
					create_time: Date.parse(new Date()),
					income: income,
					consume: consume,
					desc: desc,
					op_change: op_change,
					change: change
				}
				uniCloud.callFunction({
					name: 'add',
					data: {data: data}
				}).then(function (res) {
					if (res.success) {
						uni.showToast({
							title: '添加成功',
							icon:"none",
							mask:true
						});
						setTimeout(function () {
							that.form.change = null
							that.form.type = null
							that.form.chinese_type = null
							that.form.desc = null
						}, 1500)
					} else {
						uni.showToast({
							title: '添加失败',
							icon:"none",
							mask:true
						});
					}
				})
			}
		}
	}
</script>

<style>

</style>
